//package com.qzsoft.fileServers.serviceimpl;
//
//import com.mongodb.BasicDBObject;
//import com.mongodb.DBObject;
//import com.mongodb.gridfs.GridFSDBFile;
//import com.mongodb.gridfs.GridFSFile;
//import com.qzsoft.fileServers.service.GridFSService;
//import com.qzsoft.fileServers.utils.StringUtil;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.data.mongodb.core.query.Query;
//import org.springframework.data.mongodb.gridfs.GridFsTemplate;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.BufferedInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.URLEncoder;
//import java.util.List;
//
//@Service
//public class GridFSServiceImpl implements GridFSService {
//
//    private static Logger LOGGER = Logger.getLogger(GridFSServiceImpl.class);
//    @Autowired
//    private GridFsTemplate gridFsTemplate;
//
//    @Override
//    public GridFSFile save(MultipartFile file) throws Exception {
//
//        LOGGER.info("Saving file..");
//        DBObject metaData = new BasicDBObject();
//        metaData.put("createdDate", System.currentTimeMillis());
//        InputStream inputStream = null;
//        inputStream = file.getInputStream();
//        String filename = file.getOriginalFilename();
//        GridFSFile store  = gridFsTemplate.store(inputStream, StringUtil.getFileNameNoEx(filename), filename.substring(filename.lastIndexOf(".") + 1), metaData);
//        LOGGER.info("File saved: " + store.getId());
//       return store;
//    }
//
//    @Override
//    public void get(String id, HttpServletResponse res){
////        LOGGER.info("Getting file.." + fileName);
////        List<GridFSDBFile> result = gridFsTemplate
////                .find(new Query().addCriteria(Criteria.where("filename").is(fileName)));
////        if (result == null || result.size() == 0) {
////            LOGGER.info("File not found" + fileName);
////            throw new RuntimeException("No file with name: " + fileName);
////        }
////        LOGGER.info("File found " + fileName);
////        return IOUtils.toByteArray(result.get(0).getInputStream());
//        List<GridFSDBFile> result = gridFsTemplate
//                .find(new Query().addCriteria(Criteria.where("_id").is(id)));
//        BufferedInputStream bis = null;
//        OutputStream os = null;
//        try {
//            res.setHeader("content-type", "application/octet-stream");
//            res.setContentType("application/octet-stream");
//            res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(result.get(0).getFilename(),"UTF-8") + "." + result.get(0).getContentType());
//            byte[] buff = new byte[1024];
//            os = res.getOutputStream();
//            bis = new BufferedInputStream(result.get(0).getInputStream());
//            int i = bis.read(buff);
//            while (i != -1) {
//                os.write(buff, 0, buff.length);
//                os.flush();
//                i = bis.read(buff);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (bis != null) {
//                try {
//                    bis.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        LOGGER.info("File get: " + id);
//    }
//
//    @Override
//    public void delete(String id) {
//        LOGGER.info("Deleting file.." + id);
//        gridFsTemplate.delete(new Query().addCriteria(Criteria.where("_id").is(id)));
//        LOGGER.info("File deleted " + id);
//    }
//}
